using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SpellerCore
{
    class HardCodedPathCorrectnessProviderFactory : ICorrectnessProviderFactory
    {
        private static string _spellCenterDirectory;

        #region ICorrectnessProviderFactory Members

        public IEnumerable<ICorrectnessProvider> GetCorrectnessProvidersForWritingSystem(string writingSystemId)
        {
            List<ICorrectnessProvider> providers = new List<ICorrectnessProvider>();
            string writingSystemPath = Path.Combine(BaseDirectory, writingSystemId);
            if(!Directory.Exists(writingSystemPath))
            {
                Directory.CreateDirectory(writingSystemPath);
            }
            string providerPath = Path.Combine(writingSystemPath, "HunSpell");
            string affixFile = Path.Combine(providerPath, writingSystemId + ".aff");
            string dictionaryFile = Path.Combine(providerPath, writingSystemId + ".dic");
            if (!Directory.Exists(providerPath) || !File.Exists(affixFile) 
                || !File.Exists(dictionaryFile))
            {
                
                return providers;
            }
            HunSpellWrapper hunSpellWrapper = new HunSpellWrapper(affixFile,dictionaryFile);
            providers.Add(hunSpellWrapper);
            return providers;
        }

        /// <summary>
        /// The base directory where all the spellcenter related files exist.
        /// </summary>
        /// <remarks>SpellCenter directory will be automatically appended to the path on setting
        /// </remarks>
        internal
       static string BaseDirectory
        {
            get
            {
                if (String.IsNullOrEmpty(_spellCenterDirectory))
                {
                    BaseDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

                }
                return _spellCenterDirectory;
            }
            set
            {
                _spellCenterDirectory = Path.Combine(value, "SpellCenter");
                if (!Directory.Exists(_spellCenterDirectory))
                {
                    Directory.CreateDirectory(_spellCenterDirectory);
                }
            }
        }

        #endregion
    }
}
